using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
namespace Senparc.Weixin.MP.TenPayLib
{
	public class TenPayHttpClient
	{
		private string ReqContent;
		private string ResContent;
		private string Method;
		private string ErrInfo;
		private string CertFile;
		private string CertPasswd;
		private string CaFile;
		private int TimeOut;
		private int ResponseCode;
		private string Charset;
		public TenPayHttpClient()
		{
			this.CaFile = "";
			this.CertFile = "";
			this.CertPasswd = "";
			this.ReqContent = "";
			this.ResContent = "";
			this.Method = "POST";
			this.ErrInfo = "";
			this.TimeOut = 60;
			this.ResponseCode = 0;
			this.Charset = "gb2312";
		}
		public void SetReqContent(string reqContent)
		{
			this.ReqContent = reqContent;
		}
		public string GetResContent()
		{
			return this.ResContent;
		}
		public void SetMethod(string method)
		{
			this.Method = method;
		}
		public string GetErrInfo()
		{
			return this.ErrInfo;
		}
		public void SetCertInfo(string certFile, string certPasswd)
		{
			this.CertFile = certFile;
			this.CertPasswd = certPasswd;
		}
		public void SetCaInfo(string caFile)
		{
			this.CaFile = caFile;
		}
		public void SetTimeOut(int timeOut)
		{
			this.TimeOut = timeOut;
		}
		public int GetResponseCode()
		{
			return this.ResponseCode;
		}
		public void SetCharset(string charset)
		{
			this.Charset = charset;
		}
		public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
		{
			return true;
		}
		public bool Call()
		{
			HttpWebResponse httpWebResponse = null;
			bool result;
			try
			{
				string text = null;
				HttpWebRequest httpWebRequest;
				if (this.Method.ToUpper() == "POST")
				{
					string[] array = Regex.Split(this.ReqContent, "\\?");
					httpWebRequest = (HttpWebRequest)WebRequest.Create(array[0]);
					if (array.Length >= 2)
					{
						text = array[1];
					}
				}
				else
				{
					httpWebRequest = (HttpWebRequest)WebRequest.Create(this.ReqContent);
				}
				ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(this.CheckValidationResult);
				if (this.CertFile != "")
				{
					httpWebRequest.ClientCertificates.Add(new X509Certificate2(this.CertFile, this.CertPasswd));
				}
				httpWebRequest.Timeout = this.TimeOut * 1000;
				Encoding encoding = Encoding.GetEncoding(this.Charset);
				if (text != null)
				{
					byte[] bytes = encoding.GetBytes(text);
					httpWebRequest.Method = "POST";
					httpWebRequest.ContentType = "application/x-www-form-urlencoded";
					httpWebRequest.ContentLength = (long)bytes.Length;
					Stream requestStream = httpWebRequest.GetRequestStream();
					requestStream.Write(bytes, 0, bytes.Length);
					requestStream.Close();
				}
				httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), encoding);
				this.ResContent = streamReader.ReadToEnd();
				streamReader.Close();
				httpWebResponse.Close();
			}
			catch (Exception ex)
			{
				this.ErrInfo += ex.Message;
				if (httpWebResponse != null)
				{
					this.ResponseCode = Convert.ToInt32(httpWebResponse.StatusCode);
				}
				result = false;
				return result;
			}
			this.ResponseCode = Convert.ToInt32(httpWebResponse.StatusCode);
			result = true;
			return result;
		}
	}
}
